﻿using System.Xml.Linq;
using System.Linq;
using Silent.API.Ukrbash.Model;

namespace Silent.API.Ukrbash
{
    public static class Converter
    {
        public static BashQuote XElementToQuote(XElement item)
        {
            var quote = new BashQuote();
            var id = item.Element("id");
            if (id != null)
                quote.QuoteId = int.Parse(id.Value);
            var status = item.Element("status");
            if (status != null)
                quote.Status = (BashStatus)uint.Parse(status.Value);
            var addDate = item.Element("add_date");
            if (addDate != null)
                quote.AddDate = Utility.FromUnixTime(long.Parse(addDate.Value));
            var pubDate = item.Element("pub_date");
            if (pubDate != null)
                quote.PublishDate = Utility.FromUnixTime(long.Parse(pubDate.Value));
            var author = item.Element("author");
            if (author != null)
                quote.Author = author.Value;
            var authorId = item.Element("author_id");
            if (authorId != null)
                quote.AuthorId = int.Parse(authorId.Value);
            var text = item.Element("text");
            if (text != null)
                quote.Text = text.Value;
            var rating = item.Element("rating");
            if (rating != null)
                quote.Rating = int.Parse(rating.Value);
            quote.Tags = item.Descendants("tag").Select(XElementToQuoteTag).ToArray();
            return quote;
        }
        public static BashPicture XElementToPicture(XElement item)
        {
            var picture = new BashPicture();
            var id = item.Element("id");
            if (id != null)
                picture.PictureId = int.Parse(id.Value);
            var status = item.Element("status");
            if (status != null)
                picture.Status = (BashStatus)int.Parse(status.Value);
            var addDate = item.Element("add_date");
            if (addDate != null)
                picture.AddDate = Utility.FromUnixTime(long.Parse(addDate.Value));
            var pubDate = item.Element("pub_date");
            if (pubDate != null)
                picture.PublishDate = Utility.FromUnixTime(long.Parse(pubDate.Value));
            var author = item.Element("author");
            if (author != null)
                picture.Author = author.Value;
            var authorId = item.Element("author_id");
            if (authorId != null)
                picture.AuthorId = int.Parse(authorId.Value);
            var image = item.Element("image");
            if (image != null)
                picture.Image = image.Value;
            var thumbnail = item.Element("thumbnail");
            if (thumbnail != null)
                picture.Thumbnail = thumbnail.Value;
            var title = item.Element("title");
            if (title != null)
                picture.Title = title.Value;
            var rating = item.Element("rating");
            if (rating != null)
                picture.Rating = int.Parse(rating.Value);
            return picture;
        }
        public static BashError XElementToError(XElement item)
        {
            var error = new BashError();
            var code = item.Element("code");
            if (code != null)
                error.Code = uint.Parse(code.Value);
            var text = item.Element("text");
            if (text != null)
                error.Text = text.Value;
            return error;
        }
        public static BashSiteInfo XElementToSiteInfo(XElement item)
        {
            var siteInfo = new BashSiteInfo();
            var quotesPub = item.Element("quotes_published");
            if (quotesPub != null)
                siteInfo.QuotesPublished = int.Parse(quotesPub.Value);
            var quotesUpc = item.Element("quotes_upcoming");
            if (quotesUpc != null)
                siteInfo.QuotesUpcoming = int.Parse(quotesUpc.Value);
            var quotesPubToday = item.Element("quotes_published_today");
            if (quotesPubToday != null)
                siteInfo.QuotesPublishedToday = int.Parse(quotesPubToday.Value);
            var quotesUpcToday = item.Element("quotes_upcoming_today");
            if (quotesUpcToday != null)
                siteInfo.QuotesUpcomingToday = int.Parse(quotesUpcToday.Value);
            var picturesPub = item.Element("pictures_published");
            if (picturesPub != null)
                siteInfo.PicturesPublished = int.Parse(picturesPub.Value);
            var picturesUpc = item.Element("pictures_upcoming");
            if (picturesUpc != null)
                siteInfo.PicturesUpcoming = int.Parse(picturesUpc.Value);
            var picturesPubToday = item.Element("pictures_published_today");
            if (picturesPubToday != null)
                siteInfo.PicturesPublishedToday = int.Parse(picturesPubToday.Value);
            var picturesUpcToday = item.Element("pictures_upcoming_today");
            if (picturesUpcToday != null)
                siteInfo.PicturesUpcomingToday = int.Parse(picturesUpcToday.Value);
            var users = item.Element("users");
            if (users != null) siteInfo.Users = int.Parse(users.Value);
            var mood = item.Element("mood");
            if (mood != null) siteInfo.Mood = int.Parse(mood.Value);
            return siteInfo;
        }
        public static BashUser XElementToUser(XElement item)
        {
            var user = new BashUser();
            var id = item.Element("id");
            if (id != null)
                user.UserId = int.Parse(id.Value);
            var name = item.Element("name");
            if (name != null)
                user.Name = name.Value;
            var group = item.Element("group");
            if (group != null)
                user.Group = group.Value;
            var joined = item.Element("joined");
            if (joined != null)
                user.Joined = Utility.FromUnixTime(long.Parse(joined.Value));
            var karma = item.Element("karma");
            if (karma != null)
                user.Karma = double.Parse(karma.Value.Replace('.', ','));
            var quotesAll = item.Element("quotes_all");
            if (quotesAll != null)
                user.QuotesAll = int.Parse(quotesAll.Value);
            var quotesPub = item.Element("quotes_pub");
            if (quotesPub != null)
                user.QuotesPublished = int.Parse(quotesPub.Value);
            var picturesAll = item.Element("pictures_all");
            if (picturesAll != null)
                user.PicturesAll = int.Parse(picturesAll.Value);
            var picturesPub = item.Element("pictures_pub");
            if (picturesPub != null)
                user.PicturesPublished = int.Parse(picturesPub.Value);
            return user;
        }
        public static BashTag XElementToTag(XElement item)
        {
            var tag = new BashTag();
            var id = item.Element("id");
            if (id != null)
                tag.TagId = int.Parse(id.Value);
            var name = item.Element("name");
            if (name != null)
                tag.Name = name.Value;
            var url = item.Element("url");
            if (url != null)
                tag.Url = url.Value;
            var quotes = item.Element("quotes");
            if (quotes != null)
                tag.Quotes = int.Parse(quotes.Value);
            return tag;
        }
        public static BashTag XElementToQuoteTag(XElement item)
        {
            var tag = new BashTag
                {
                    TagId = int.Parse(item.Attribute("id").Value),
                    Name = item.Value
                };
            return tag;
        }
    }
}
